package com.luyu.service.impl;

import com.luyu.service.IMailService;
import com.luyu.service.config.MyJavaMailSenderImpl;
import jakarta.annotation.Resource;
import jakarta.mail.MessagingException;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;


@Service
public class MailServiceImpl implements IMailService {
    @Resource
    private JavaMailSender mailSender;
    @Autowired
    private JavaMailSenderImpl sender;
    @Override
    public void sendHtmlMailThymeLeaf(String mailFrom,
                                      String mailFromNick,
                                      String mailTo,
                                      String cc,
                                      String subject,
                                      String content) throws MessagingException {
        MimeMessage mimeMessage = mailSender.createMimeMessage();

        try {
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, false);
            mimeMessageHelper.setFrom(mailFrom);
            // 设置收件人
            mimeMessageHelper.setTo(mailTo);
            mimeMessageHelper.setText(content);
            mailSender.send(mimeMessage);
        } catch (MessagingException e) {
            System.out.println(e);
        }
    }

    @Override
    public void sendHtmlMailThymeLeaf1(String mailFrom, String mailFromNick, String mailTo,  String subject, String content) throws MessagingException {
        MimeMessage msg  = sender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(msg);
        helper.setFrom(mailFrom);
        helper.setTo(mailTo);
        helper.setText(content);
        helper.setSubject(subject);
        sender.send(msg);
        System.out.println("邮箱发送成功");
    }


    /**
     * 1、发送普通文本邮件
     *
     * @param mailFrom     发件人邮箱
     * @param mailFromNick 发件人昵称
     * @param mailTo       收件人邮箱
     * @param subject      主题
     * @param content      内容
     */
    @Override
    public void sendSimpleMail(String mailFrom, String mailFromNick, String mailTo,
                               String subject, String content) {
        try {
            // 多个收件人之间用英文逗号分隔
            String[] mailToArr = mailTo.split(",");
            for (String address : mailToArr) {
                // 简单邮件信息类
                MimeMessage mimeMessage = mailSender.createMimeMessage();
                // HTML邮件信息类
                MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage);
                // 昵称
                mimeMessageHelper.setFrom(new InternetAddress(mailFromNick + " <" + mailFrom + ">"));
                mimeMessageHelper.setTo(address);
                mimeMessageHelper.setSubject(subject);
                mimeMessageHelper.setText(content);
                mailSender.send(mimeMessage);
            }
        } catch (Exception e) {
            System.out.println("发送邮件失败："+ e);
        }
    }
    @Autowired
    private MyJavaMailSenderImpl myJavaMailSender;
    @Override
    public void sendMySimpleMail(String mailFrom, String mailFromNick, String mailTo,
                               String subject, String content) {
        System.out.println("配置:"+myJavaMailSender.getJavaMailProperties());
        try {
            // 多个收件人之间用英文逗号分隔
            String[] mailToArr = mailTo.split(",");
            for (String address : mailToArr) {
                // 简单邮件信息类
                MimeMessage mimeMessage = mailSender.createMimeMessage();
                // HTML邮件信息类
                MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage);
                // 昵称
                mimeMessageHelper.setFrom(new InternetAddress(mailFromNick + " <" + mailFrom + ">"));
                mimeMessageHelper.setTo(address);
                mimeMessageHelper.setSubject(subject);
                mimeMessageHelper.setText(content);
                mailSender.send(mimeMessage);
            }
        } catch (Exception e) {
            System.out.println("发送邮件失败："+ e);
        }
    }
}
